﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DAO;

namespace Sklad.WindowsApplication
{
    public partial class ReceiveForm : Form
    {
        public static DaoRepository dr = new DaoRepository();
        public static BindingSource bs = new BindingSource();

        public ReceiveForm()
        {
            InitializeComponent();
            this.bindingSource_recieve.DataSource = new BindingList<Acceptance>(dr.AcceptanceRepository.GetAll().ToList());
            this.bindingNavigator_receive.BindingSource = this.bindingSource_recieve;
            LinkElements();
        }

        private void LinkElements()
        {
            this.bindingNavigator_receive.BindingSource = this.bindingSource_recieve;
            this.tb_rec_id.DataBindings.Add("text", this.bindingSource_recieve, "Id");
            this.dp_receive.DataBindings.Add("text", this.bindingSource_recieve, "Date");
            this.tb_rec_empl.DataBindings.Add("text", this.bindingSource_recieve, "Employer");
            this.tb_provider.DataBindings.Add("text", this.bindingSource_recieve, "Provider");
            this.dataGridView1.DataSource = bs;
            UpdateDataGrid();
            this.bindingSource_recieve.CurrentChanged += bindingSource_recieve_CurrentChanged;
        }

        void bindingSource_recieve_CurrentChanged(object sender, EventArgs e)
        {
            UpdateDataGrid();
        }

        void UpdateDataGrid()
        {
            var temp = this.bindingSource_recieve.Current as Acceptance;
            if (temp == null)
            {
                return;
            }
            var templist = from Acceptance_Product in temp.Acceptance_Product
                           select new
                           {
                               Acceptance_Product.Product.Name,
                               Acceptance_Product.Count
                           };

            bs.DataSource = templist;
        }

        private void UpdateData()
        {
            this.bindingSource_recieve.DataSource = new BindingList<Acceptance>(dr.AcceptanceRepository.GetAll().ToList());
        }

        private void bt_add_provider_Click(object sender, EventArgs e)
        {
            AddProviderForm apf = new AddProviderForm();
            apf.Visible = true;
            apf.FormClosing += apf_FormClosing;
            this.Visible = false;
        }

        void apf_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Visible = true;
        }

        private void bt_add_rec_Click(object sender, EventArgs e)
        {
            AddReceiveForm adf = new AddReceiveForm();
            adf.Visible = true;
            adf.FormClosing += adf_FormClosing;
            this.Visible = false;
        }

        void adf_FormClosing(object sender, FormClosingEventArgs e)
        {
            AddReceiveForm.itemlist = null;
            UpdateData();
            UpdateDataGrid();
            this.Visible = true;
        }
    }
}
